function addText() {
	//Send text that player has wrote to chat input
	var userInput = document.getElementById('userInput').value;
	$.post("Engine?chat="+userInput);
}

function showchathistory(){
	if ($("#chat").css("height")=="500px"){
		$("#chat").animate({"top": "533px","height":"6em","width":"20em"},1000);
	}
	else{
		$("#chat").animate({"top": "100px","height":"500px","width":"30em"},1000);
		$("#chat_content").html("");
		for(i=1;i<=localStorage.length;i++){
			text = document.createElement('div');
			text.innerHTML =localStorage.getItem(i);
			$('#chat_content').append(text).animate({ scrollTop: $('#chat_content').attr("scrollHeight") }, 50);
		}
	}
}

function savetolocalhistory(text){
	localStorage.setItem(localStorage.length+1,text);
}

function clearhistory(){
	var b=confirm("Are you sure, you want to clear the history of this chat?");
	if (b==true){
		localStorage.clear();
	}
}

function activatechat(){
	var url = "chat";
    var request =  new XMLHttpRequest();
    request.open("GET", url, true);
    request.setRequestHeader("Content-Type","application/x-javascript;");
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            if (request.status == 200){
                if (request.responseText) {
                   addlog(request.responseText);
                }
            }
            activatechat();
        }
    };
    request.send(null);
}

function addlog(words){
	//Adds any kind of log information, that is displayed as Dealer:
	text = document.createElement('div');
	var date=(new Date()).toDateString()+" "+(new Date()).toTimeString().substring(0, 8);
	if (words.substring(0,1)==0){
		text.innerHTML ="["+date+"] "+ words.substring(2,words.length);
		savetolocalhistory("["+date+"] "+ words.substring(2,words.length));
	}
	else{
		var plname=$("#pl"+words.substring(0,1)+"_name").html();
		text.innerHTML = "["+date+"] "+"player "+plname+" "+words.substring(2,words.length);
		savetolocalhistory("["+date+"] "+"player "+plname+" "+words.substring(2,words.length));
	}
	
	$('#chat_content').append(text).animate({ scrollTop: $('#chat_content').attr("scrollHeight") }, 500);
}

function exitwindow(){
	//Function to exit the game(works only if it is open by javascript)
	var x=confirm("Are you sure you want to exit?");
	if (x==true){
		window.close();
	}
}

function addyourname(){
	//Function to replace the name of the player at start of the game
	var name = prompt("What is your name?", $("#pl1_name").html());
	$("#pl1_name").html(name);
}
function pausecomp(millis){
	//A delay for the script
	var date = new Date();
	var curDate = null;
	do { curDate = new Date(); }
	while(curDate-date < millis);
}